import * as types from './mutations-types'

export default {
  addCart ({ state, commit }, payload) {
    return new Promise((resolve, reject) => {
      // 查找数组中是否有该对象
      const item = state.cartGoodsList.find(t => t.iid === payload.iid)
      // // 三木写法, promise不支持这种写法
      // item ? commit(types.ADD_GOODS_NUMBER, item) && resolve('当前商品数量+1') : (payload.count = 1) && commit(types.ADD_GOODS_TYPE, payload) && resolve('添加了新的商品')

      // if语句
      if (item) {
        commit(types.ADD_GOODS_NUMBER, item)
        resolve('当前商品数量+1')
      } else {
        (payload.count = 1) && commit(types.ADD_GOODS_TYPE, payload)
        resolve('添加了新的商品')
      }
    })
  }
}
